`
https://leetcode.cn/problems/delete-nodes-and-return-forest/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number[]} to_delete
 * @return {TreeNode[]}
 */
var delNodes = function (root, to_delete) {
  const delSet = new Set(to_delete)
  const res = []

  // 传入一棵二叉树，删除其中值在 delSet 里面的结点，删除完成后返回根节点
  const doDelete = (root, hasParent) => {
    if (root === null) {
      return null
    }

    // 如果没有父元素，也不需要被删除
    const isDeleted = delSet.has(root.val)
    if (!hasParent && !isDeleted) {
      // 就是一个新的树
      res.push(root)
    }

    root.left = doDelete(root.left, !isDeleted)
    root.right = doDelete(root.right, !isDeleted)

    // 如果需要删除，返回 null 给父节点
    return isDeleted ? null : root
  }

  doDelete(root, false)
  return res
};